<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { authApi } from '../../services/api'

// 表单数据
const loginForm = ref({
  username: '',
  password: ''
})

const router = useRouter()
const loading = ref(false)
const errorMessage = ref('')

// 登录方法
const handleLogin = async (): Promise<void> => {
  if (!loginForm.value.username || !loginForm.value.password) {
    errorMessage.value = '请输入用户名和密码'
    return
  }
  
  try {
    loading.value = true
    errorMessage.value = ''
    
    // 调用后端登录接口
    await authApi.login({
      username: loginForm.value.username,
      password: loginForm.value.password
    })

    // 登录成功后跳转到主页
    router.push('/home')
  } catch (err) {
    errorMessage.value = '登录失败: ' + (err as Error).message
    console.error('登录失败:', err)
  } finally {
    loading.value = false
  }
}

// 跳转到注册页面
const goToRegister = () => {
  router.push('/register')
}
</script>

<template>
  <div class="login-container">

    
    <div class="login-form neo-brutalist">
      <h2>用户登录</h2>
      
      <div v-if="errorMessage" class="error-message neo-brutalist">
        {{ errorMessage }}
      </div>
      
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            id="username"
            v-model="loginForm.username"
            type="text" 
            placeholder="请输入用户名"
            :disabled="loading"
            class="neo-brutalist-input"
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            id="password"
            v-model="loginForm.password"
            type="password" 
            placeholder="请输入密码"
            :disabled="loading"
            class="neo-brutalist-input"
          />
        </div>
        
        <button 
          type="submit" 
          class="login-button neo-brutalist btn btn--primary"
          :disabled="loading"
          :class="{ 'btn--disabled': loading }"
        >
          {{ loading ? '登录中...' : '登录' }}
        </button>
      </form>
      
      <div class="form-footer">
        <p>还没有账号？<a  @click.prevent="goToRegister">立即注册</a></p>
      </div>
    </div>
  </div>
</template>

<style scoped>
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 2rem 1rem;
}

/* 导航栏样式 */
.navbar {
  width: 100%;
  max-width: 800px;
  padding: 1rem 1.5rem;
  margin-bottom: 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
}

.navbar-brand {
  font-size: 1.5rem;
  font-weight: bold;
  color: #000;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 2px;
}

.navbar-right {
  display: flex;
  align-items: center;
}

.login-form {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: #fff;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #000;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 2px;
  font-size: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  color: #000;
  font-weight: 500;
  font-family: 'Bebas Neue', sans-serif;
  letter-spacing: 1px;
  font-size: 1.1rem;
  width: 100%;
}

.neo-brutalist-input {
  width: 100%;
  padding: 0.75rem;
  font-size: 1rem;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  font-family: 'Roboto Mono', monospace;
  background-color: #fff;
  box-sizing: border-box;
}

.neo-brutalist-input:focus {
  outline: none;
  box-shadow: 2px 2px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist-input:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.form-footer {
  text-align: center;
  margin-top: 1.5rem;
}

.form-footer a {
  color: var(--neo-brutalist-blue);
  text-decoration: none;
  font-weight: bold;
}

.form-footer a:hover {
  color: var(--neo-brutalist-red);
}

/* .error-message {
  padding: 0.75rem;
  background-color: var(--neo-brutalist-red);
  color: #fff;
  margin-bottom: 1rem;
  text-align: center;
  font-weight: bold;
} */

/* Neo Brutalist Styles */
.neo-brutalist {
  border: 4px solid #000;
  box-shadow: 8px 8px 0 #000;
  transition: all 0.2s ease;
  border-radius: 4px;
  background-color: #fff;
}

.neo-brutalist:hover {
  box-shadow: 6px 6px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist.btn {
  font-weight: bold;
  padding: 0.75rem 1.5rem;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  transition: none;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.1rem;
  border: 4px solid #000;
  box-shadow: 4px 4px 0 #000;
  width: 100%;
  box-sizing: border-box;
}

.neo-brutalist.btn:hover {
  background-color: var(--neo-brutalist-yellow);
  color: #000;
  box-shadow: 2px 2px 0 #000;
  transform: translate(2px, 2px);
}

.neo-brutalist.btn--primary {
  background-color: var(--neo-brutalist-yellow);
  color: #000;
}

.neo-brutalist.btn.btn--disabled {
  background-color: #ccc;
  cursor: not-allowed;
  box-shadow: 4px 4px 0 #999;
}

.neo-brutalist.btn.btn--disabled:hover {
  box-shadow: 4px 4px 0 #999;
  transform: none;
}
</style>